package com.eqdd.yiqidian.http;


import android.text.TextUtils;

import com.eqdd.yiqidian.base.BaseActivity;
import com.eqdd.yiqidian.base.BasePresenter;
import com.eqdd.yiqidian.base.Config;
import com.eqdd.yiqidian.utils.ToastUtil;

import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import okhttp3.RequestBody;
import rx.Observable;
import rx.Subscriber;

/**
 * Created by lvzhihao on 17-3-28.
 */

public class HttpPresneter<T> extends BasePresenter {
    Class<T> clazz;
    BaseActivity baseView;
    Map<String, String> params = new HashMap();
    Map<String, String> headers = new HashMap();
    Map<String, RequestBody> partMap = new HashMap<>();
    Map<String, String> fileMap = new HashMap();
    String url;
    OnHttpListener<T> onHttpListener;
    Type type;

    private HttpPresneter(Class<T> clazz, BaseActivity baseView,
                          Map<String, String> params, Map<String, String> headers,
                          String url, OnHttpListener onHttpListener,Type type,
                          Map<String, RequestBody> partMap,Map<String,String> fileMap) {
        this.baseView = baseView;
        this.clazz = clazz;
        this.headers = headers;
        this.params = params;
        this.onHttpListener = onHttpListener;
        this.url = url;
        this.type=type;
        this.partMap=partMap;
        this.fileMap=fileMap;

    }

    private void doHttp(int mode) {
        Subscriber<T> subscriber = new Subscriber<T>() {

            @Override
            public void onStart() {
                super.onStart();
                if (baseView!=null) {
                    baseView.showLoading();
                }

            }

            @Override
            public void onCompleted() {
                if (baseView!=null) {

                    baseView.hideLoading();
                }
            }

            @Override
            public void onError(Throwable e) {
                System.out.println("错误"+e.toString());
                if (baseView!=null) {

                    baseView.hideLoading();
                }

                onHttpListener.onError(e);
                ToastUtil.showShort(Config.SERVER_ERROR);


            }

            @Override
            public void onNext(T t) {
                onHttpListener.onNext(t);
            }
        };
        HttpMethods.getInstance().getDataFromServer(baseView, subscriber, clazz, url, headers, params, mode);
    }

    private Observable<List<T>> doObserverHttpList(int mode) {
       return HttpMethods.getInstance().getObserverDataFromServer(baseView, clazz, url, headers, params, mode);
    }

    private Observable<T> doObserverHttpBean(int mode) {
        return HttpMethods.getInstance().getObserverDataFromServerBean(baseView, clazz, url, headers, params, mode);
    }

    public void get() {
        doHttp(0);
    }

    public void post() {
        doHttp(1);
    }
    public Observable<List<T>> getObserverList() {
        return doObserverHttpList(0);
    }

    public Observable<T> postObserverBean() {
        return doObserverHttpBean(1);
    }

    public Observable<T> getObserverBean() {
        return doObserverHttpBean(0);
    }

    public Observable<List<T>> postObserverList() {
        return doObserverHttpList(1);
    }
    public void formDataPost(){
        Subscriber<T> subscriber = new Subscriber<T>() {

            @Override
            public void onStart() {
                super.onStart();
                if (baseView!=null) {
                    baseView.showLoading();
                }

            }

            @Override
            public void onCompleted() {
                if (baseView!=null) {

                    baseView.hideLoading();
                }
            }

            @Override
            public void onError(Throwable e) {
                System.out.println("错误"+e.toString());
                if (baseView!=null) {
                    baseView.hideLoading();
                }
                onHttpListener.onError(e);
                ToastUtil.showShort(Config.SERVER_ERROR);
            }

            @Override
            public void onNext(T t) {
                onHttpListener.onNext(t);
            }
        };
         HttpMethods.getInstance().getDataFromServerMultipart(baseView, subscriber,clazz, url, headers,partMap,fileMap);
    }

    public interface OnHttpListener<T> {
        void onError(Throwable e);

        void onNext(T t);
    }

    private HttpPresneter(Builder builder) {
        this.url = builder.url;
        this.params = builder.params;
        this.headers = builder.headers;
        this.onHttpListener = builder.onHttpListener;
        this.clazz = builder.clazz;
        this.baseView = builder.baseView;
        this.type=builder.type;
        this.partMap=builder.partMap;
        this.fileMap=builder.fileMap;

    }

    public static class Builder<T> {
        Map<String, String> params = new HashMap();
        Map<String, RequestBody> partMap = new HashMap();
        Map<String, String> fileMap = new HashMap();

        Map<String, String> headers = new HashMap();
        String url;
        OnHttpListener<T> onHttpListener;
        Class<T> clazz;
        BaseActivity baseView;
        Type type;

        public Builder CacheTime(String time) {
            this.headers.put("Cache-Time", time);
            return this;
        }
        public Builder Url(String url) {
            this.url = url;
            return this;
        }
        public Builder Type(Type type) {
            this.type = type;
            return this;
        }

        public Builder Params(Map<String, String> params) {
            this.params.putAll(params);
            return this;
        }

        public Builder Params(String key, String value) {
            this.params.put(key, value);
            return this;
        }

        public Builder PartMap(String key, String value) {
            this.partMap.put(key, HttpMultiUtil.createPartFromString(value));
            return this;
        }

        public Builder fileMap(String param,String filePath){
            this.fileMap.put(param,filePath);
            return this;
        }
        public Builder Headers(Map<String, String> headers) {
            this.headers.putAll(headers);
            return this;
        }

        public Builder Headers(String key, String value) {
            this.headers.put(key, value);
            return this;
        }

        public Builder setOnHttpListener(OnHttpListener<T> onHttpListener) {
            this.onHttpListener = onHttpListener;
            return this;
        }

        public Builder(String url, Class<T> clazz, BaseActivity baseView) {
            this.clazz = clazz;
            this.baseView = baseView;
            this.url = url;
            this.params = new HashMap();
            this.headers = new HashMap();
            this.partMap = new HashMap();
        }

        private String checkUrl(String url) {
            if (checkNULL(url)) {
                throw new NullPointerException("absolute url can not be empty");
            }
            return url;
        }

        public static boolean checkNULL(String str) {
            return str == null || "null".equals(str) || "".equals(str);
        }
        public HttpPresneter build() {
            this.url = this.checkUrl(this.url);
            this.params = checkParams(this.params);
            this.headers= checkHeaders(this.headers);
            return new HttpPresneter<String>(this);
        }


        public static Map<String, String> checkParams(Map<String, String> params) {
            if(params == null) {
                params = new HashMap();
            }
            Iterator var1 = ((Map)params).entrySet().iterator();

            while(var1.hasNext()) {
                Map.Entry entry = (Map.Entry)var1.next();
                if(entry.getValue() == null) {
                    ((Map)params).put(entry.getKey(), "");
                }
            }

            return params;
        }

        public static Map<String, String> checkHeaders(Map<String, String> headers) {
            if (headers == null) {
                headers = new HashMap();
            }
            Iterator var1 = ((Map) headers).entrySet().iterator();

            while (var1.hasNext()) {
                Map.Entry entry = (Map.Entry) var1.next();
                if (entry.getValue() == null) {
                    ((Map) headers).put(entry.getKey(), "");
                }
            }
            return headers;
        }
    }
}
